// 导入 defineStore，解决找不到名称“defineStore”的问题
import { defineStore } from 'pinia';
import { useCookie } from 'nuxt/app'
import type { UserInfo, ApiResponse } from '../types/common'
export const useUserStore = defineStore('userStore', {
    state: () => ({
      nickname: '',
      avatar: '',
      id: 0
    }),
    actions: {
      setUser(user: UserInfo) {
        this.nickname = user.nickname;
        this.avatar = user.avatar;
        this.id = user.id;
      },
      async getUserInfo(){
        const data = await useApi<ApiResponse<UserInfo>>('/api/user-info')
        if(data.code === 200){
          console.log(data.data)
        }
      },
      clearTokens() {
        const accessTokenCookie = useCookie('accessToken')
        const refreshTokenCookie = useCookie('refreshToken')
        accessTokenCookie.value = null
        refreshTokenCookie.value = null
      }
    }
  })
  